package com.ljd.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.ljd.music.pojo.Song;
import com.ljd.music.service.SongService;
import com.ljd.music.util.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/song")
public class SongController {

    @Autowired
    private SongService songService;


    @PostMapping("add")
    public Object addSong(Song song, @RequestParam("file") MultipartFile multipartFile) {
        JSONObject jsonObject = new JSONObject();

        System.out.println(song);
        System.out.println(multipartFile);

        // 歌曲图片设置为默认的图片地址
        song.setPic("/images/songPic/歌曲默认图标.jpg");

        // 判断上传的歌曲文件是否为空
        if (multipartFile.isEmpty()) {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "歌曲文件不存在");
            return jsonObject;
        }

        // 歌曲文件名
        String fileName = System.currentTimeMillis() + multipartFile.getOriginalFilename();
        // 歌曲文件夹路径
        String filePath = System.getProperty("user.dir") + System.getProperty("file.separator") + "song";
        File file = new File(filePath);
        // 是否存在
        if (!file.exists()) {
            file.mkdirs();
        }

        // 歌曲文件地址
        File dest = new File(filePath + System.getProperty("file.separator") + fileName);
        // 数据库存入的地址
        String storePath = "/song/" + fileName;
        try {
            // 文件上传
            multipartFile.transferTo(dest);
            // 将数据库地址存入到实体类中
            song.setUrl(storePath);
            // 添加数据
            boolean insert = songService.insert(song);
            if (insert) {
                jsonObject.put(Consts.CODE, 1);
                jsonObject.put(Consts.MSG, "添加歌曲成功");
                jsonObject.put("avator", storePath);
            } else {
                jsonObject.put(Consts.CODE, 0);
                jsonObject.put(Consts.MSG, "添加歌曲失败");
            }
            return jsonObject;
        } catch (IOException e) {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "上传歌曲文件失败");
            return jsonObject;
        } finally {
            return jsonObject;
        }


    }

    @GetMapping("allSongs")
    public Object allSongs(Integer singerId) {
        return songService.selectSongOfSingerId(singerId);
    }

    @GetMapping("all")
    public Object all() {
        return songService.allSong();
    }

    @PostMapping("update")
    public Object updateSong(Song song) {
        JSONObject jsonObject = new JSONObject();

        System.out.println(song);
        boolean update = songService.update(song);

        if (update) {
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "修改成功");

        } else {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "修改失败");
        }
        return jsonObject;
    }

    @PostMapping("delete")
    public Object deleteSong(Integer id) {

        Song song = songService.selectByPrimaryKey(id);

        String url = song.getUrl();

        String[] split = url.split("/");
        String songName = split[split.length - 1];
        File file = new File(System.getProperty("user.dir") + System.getProperty("file.separator") + "song"
                + System.getProperty("file.separator") + songName);

        if (!file.exists()) {
            file.delete();
        }


        JSONObject jsonObject = new JSONObject();
        boolean delete = songService.delete(id);

        if (delete) {
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "删除成功");
        } else {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "删除失败");
        }
        return jsonObject;
    }

    @GetMapping("deleteSongs")
    public Object deleteSongs (String ids) {
        JSONObject jsonObject = new JSONObject();

        List<String> singerIds = Arrays.asList(ids.split(","));
        System.out.println(singerIds);
        boolean delete = songService.deleteSongs(singerIds);

        if(delete) {
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"删除成功");
        }else {
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"删除失败");
        }
        return jsonObject;
    }

    @PostMapping("updateSongPic")
    public Object updateSongPic(Integer id, @RequestParam("file") MultipartFile multipartFile) {
        JSONObject jsonObject = new JSONObject();

        if (multipartFile.isEmpty()) {
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"文件为空");
            return jsonObject;
        }

        String fileName = System.currentTimeMillis() + multipartFile.getOriginalFilename();
        String filePath = System.getProperty("user.dir") + System.getProperty("file.separator") + "images"
                + System.getProperty("file.separator") + "songPic";

        File file = new File(filePath);
        if(!file.exists()){
            file.mkdirs();
        }

        String sqlPath = "/images/songPic/" + fileName;

        try {
            multipartFile.transferTo(new File(filePath + System.getProperty("file.separator") + fileName));
            Song song = new Song();
            song.setPic(sqlPath);
            song.setId(id);
            boolean update = songService.update(song);

            if(update) {
                jsonObject.put(Consts.CODE,1);
                jsonObject.put(Consts.MSG,"修改成功");
                return jsonObject;
            }else {
                jsonObject.put(Consts.CODE,0);
                jsonObject.put(Consts.MSG,"修改失败");
                return jsonObject;
            }
        } catch (IOException e) {
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"文件上传失败");
            return jsonObject;
        }finally {
            return jsonObject;
        }


    }

    @PostMapping("updateSongUrl")
    public Object updateSongUrl(Integer id,@RequestParam("file") MultipartFile multipartFile) {
        JSONObject jsonObject = new JSONObject();

        if (multipartFile.isEmpty()) {
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"文件为空");
            return jsonObject;
        }

        String fileName = System.currentTimeMillis() + multipartFile.getOriginalFilename();
        String filePath = System.getProperty("user.dir") + System.getProperty("file.separator") + "song";

        File file = new File(filePath);
        if(!file.exists()){
            file.mkdirs();
        }

        String sqlPath = "/song/" + fileName;

        try {
            multipartFile.transferTo(new File(filePath + System.getProperty("file.separator") + fileName));
            Song song = new Song();
            song.setUrl(sqlPath);
            song.setId(id);
            boolean update = songService.update(song);

            if(update) {
                jsonObject.put(Consts.CODE,1);
                jsonObject.put(Consts.MSG,"修改成功");
                return jsonObject;
            }else {
                jsonObject.put(Consts.CODE,0);
                jsonObject.put(Consts.MSG,"修改失败");
                return jsonObject;
            }
        } catch (IOException e) {
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"文件上传失败");
            return jsonObject;
        }finally {
            return jsonObject;
        }
    }

}
